import { http } from "@/utils/http";
import { baseAdminURLApi, type PageResult } from "../utils";

interface WikiQuery {
  title: string;
  currentPage: number;
  pageSize: number;
  isTop: number;
  isPublish: number;
}
// 新增/编辑 知识库
export interface wikiDTO {
  id: number;
  title: string;
  cover: string;
  summary: string;
  coverList: Array<any>;
}

// 分页 知识库
export interface wikiPageVO {
  id: number;
  title: string;
  cover: string;
  summary: string;
  isPublish: number;
  isTop: number;
}

export interface wikiCatalogVO {
  value: wikiCatalogVO[];
  id: number;
  icon: string;
  sort: number;
  title: string;
  child: Array<wikiCatalogVO>;
}

export interface CatalogDTO {
  wikiId: number;
  icon: string;
  title: string;
}

/** 条件分页获取知识库 */
export const getWikiList = (data?: WikiQuery) => {
  return http.request<PageResult<wikiPageVO>>("get", baseAdminURLApi("/wiki"), {
    params: data
  });
};

/** 新增知识库 */
export const addWiki = (data?: wikiDTO) => {
  return http.request<boolean>("post", baseAdminURLApi("/wiki"), {
    data
  });
};

/** 修改知识库 */
export const editWiki = (id?: number, data?: wikiDTO) => {
  return http.request<boolean>("put", baseAdminURLApi(`/wiki/${id}`), {
    data
  });
};

/** 删除知识库  传知识库状态 1、2会假删除 3会真删除*/
export const deleteWiki = (ids: Array<number>) => {
  return http.request<boolean>("delete", baseAdminURLApi("/wiki"), {
    data: ids
  });
};
/** 修改知识库置顶/启用状态 */
export const updateWikiTopOrPublish = (id, type, value) => {
  return http.request<boolean>(
    "put",
    baseAdminURLApi(`/wiki/${id}/${type}/${value}`)
  );
};

/** 根据知识库id获取知识库详细信息 */
export const getWikiById = id => {
  return http.request<wikiDTO>("get", baseAdminURLApi(`/wiki/${id}`));
};

/** 根据知识库标题 和 id 判断知识库标题是否重复了 */
export const titleExist = data => {
  return http.request<boolean>("get", baseAdminURLApi(`/wiki/check`), {
    params: data
  });
};

/** 根据知识库Id获取目录 */
export const getWikiCatalog = (id: number) => {
  return http.request<Array<wikiCatalogVO>>(
    "get",
    baseAdminURLApi(`/wiki/catalog/${id}`)
  );
};

/** 添加目录 */
export const addWikiCatalog = (data: CatalogDTO) => {
  return http.request("post", baseAdminURLApi("/catalog"), { data });
};

/** 拖拽目录调整顺序 */
export const updateSortApi = (data: Array<{ id: number; sort: number }>) => {
  return http.request("put", baseAdminURLApi("/catalog"), { data });
};

/** 根据Id获取目录详情 */
export const getCatalog = (id: number) => {
  return http.request<CatalogDTO>("get", baseAdminURLApi(`/catalog/${id}`));
};

/** 修改目录 */
export const updateCatalog = (id: number, data: CatalogDTO) => {
  return http.request("put", baseAdminURLApi(`/catalog/${id}`), { data });
};

/** 移除目录 */
export const removeCatalogById = (id: number) => {
  return http.request("delete", baseAdminURLApi(`/catalog/${id}`));
};

/** 目录添加文章 */
export const addArticle = (id: number, articleDTOS: any) => {
  return http.request("post", baseAdminURLApi(`/catalog/${id}/article`), {
    data: articleDTOS
  });
};
